package com.dlc.shop.security.multishop.controller;


import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.dlc.shop.bean.enums.PositionType;
import com.dlc.shop.common.enums.SysTypeEnum;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.security.common.bo.UidInfoBO;
import com.dlc.shop.security.common.manager.PasswordCheckManager;
import com.dlc.shop.security.common.manager.PasswordManager;
import com.dlc.shop.security.common.manager.TokenStore;
import com.dlc.shop.security.multishop.dto.CaptchaAuthenticationDTO;
import com.dlc.shop.sys.common.dao.ShopEmployeeMapper;
import com.dlc.shop.sys.common.model.ShopEmployee;
import com.dlc.shop.sys.common.service.ShopMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author FrozenWatermelon
 * @date 2020/6/30
 */
@RestController
@Tag(name = "登录")
@AllArgsConstructor
public class MultishopLoginController {

    private final TokenStore tokenStore;
    private final ShopEmployeeMapper shopEmployeeMapper;
    private final ShopMenuService shopMenuService;
    private final PasswordCheckManager passwordCheckManager;
    private final CaptchaService captchaService;
    private final PasswordManager passwordManager;

    @PostMapping("/shopLogin")
    @Operation(summary = "账号密码 + 验证码登录(用于后台登录)" , description = "通过账号/手机号/用户名密码登录")
    public ServerResponseEntity<?> login(
            @Valid @RequestBody CaptchaAuthenticationDTO captchaAuthenticationDTO) {
        // 登陆后台登录需要再校验一遍验证码
        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setCaptchaVerification(captchaAuthenticationDTO.getCaptchaVerification());
        ResponseModel response = captchaService.verification(captchaVO);
        if (!response.isSuccess()) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.user.code.error"));
        }

        ShopEmployee shopEmployee = shopEmployeeMapper.getShopByUsernameOrMobile(captchaAuthenticationDTO.getUserName());
        if (shopEmployee == null) {
            throw new YamiShopBindException("yami.user.account.error");
        }

        String decryptPassword = passwordManager.decryptPassword(captchaAuthenticationDTO.getPassWord());

        // 半小时内密码输入错误十次，已限制登录30分钟
        passwordCheckManager.checkPassword(SysTypeEnum.MULTISHOP,captchaAuthenticationDTO.getUserName(), decryptPassword, shopEmployee.getPassword());

        // 不是店铺超级管理员，并且是禁用状态，无法登录
        if (!Objects.equals(PositionType.ADMIN.value(), shopEmployee.getType()) && Objects.equals(shopEmployee.getStatus(),0)) {
            // 未找到此用户信息
            throw new YamiShopBindException("yami.shop.user.account.lock");
        }
        UidInfoBO uidInfoBO = new UidInfoBO(SysTypeEnum.MULTISHOP, shopEmployee.getEmployeeId().toString(), shopEmployee.getShopId());

        uidInfoBO.setAdmin(Objects.equals(PositionType.ADMIN.value(), shopEmployee.getType())? 1 : 0);

        return ServerResponseEntity.success(tokenStore.storeAndGetVo(uidInfoBO));
    }

}
